// ==UserScript==
// @name         必应积分商城自动搜索
// @description  必应Rewards当日任务自动完成工具
// @version      1.1.2
// @author       fifthking
// @namespace    fifthgame
// @icon         https://www.bing.com/favicon.ico
// @run-at       document-end
// @grant        GM_registerMenuCommand
// @grant        GM_xmlhttpRequest
// @grant        GM_notification
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addStyle
// @match        https://www.bing.com/*
// @match        https://cn.bing.com/*
// @connect      bing.com
// @connect      top.baidu.com
// ==/UserScript==

const KEY_COUNTER = "Counter";
const KEY_HOT_WORDS = "HotWords";
const MAX_COUNT_WINDOWS = 30;
const MAX_COUNT_MOBILE = 20;
const WINDOWS_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.200";
const MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Mobile Safari/537.36 Edg/115.0.1901.200";
let HOT_WORDS = [];

const startMenu = GM_registerMenuCommand("开始", () => { localStorage.setItem(KEY_COUNTER, 0); window.open(`https://cn.bing.com/search?q=FIFTHKING`, "_self"); }, 'o');
const stopMenu = GM_registerMenuCommand("停止", () => { localStorage.setItem(KEY_COUNTER, MAX_COUNT_WINDOWS); }, 'o');

function GetHotWords() {
    const hotData = sessionStorage.getItem(KEY_HOT_WORDS);
    if (hotData == null) {
        const request = new XMLHttpRequest();
        request.open('GET', "https://tenapi.cn/v2/baiduhot", false);
        request.send();
        if (request.status == 200) {
            const result = JSON.parse(request.responseText);
            if (result.code == 200) {
                let hotWords = [];
                for (let i = 0; i < result.data.length; i++) {
                    hotWords.push(result.data[i].name);
                }
                sessionStorage.setItem(KEY_HOT_WORDS, JSON.stringify(hotWords))
            }
        }
    }
}

// 生成指定长度的包含大写字母、小写字母和数字的随机字符串
function generateRandomString(length) {
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    let result = '';
    const charactersLength = characters.length;
    for (let i = 0; i < length; i++) {
        // 从字符集中随机选择字符，并拼接到结果字符串中
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

(function () {
    'use strict';
    let searchCounter = parseInt(localStorage.getItem(KEY_COUNTER));
    if (!isNaN(searchCounter) && searchCounter < MAX_COUNT_WINDOWS) {
        GetHotWords();
        const hotWords = JSON.parse(sessionStorage.getItem(KEY_HOT_WORDS));
        if (hotWords != null) {
            const randomInterval = Math.floor(Math.random() * 20000) + 10000;
            setTimeout(function () {
                localStorage.setItem(KEY_COUNTER, searchCounter + 1);
                const url = `https://cn.bing.com/search?q=${encodeURI(hotWords[searchCounter])}&form=${generateRandomString(4)}&cvid=${generateRandomString(32)}`;
                location.href = url;
                // window.open(url, "_self");
            }, randomInterval);
        }
    }

    document.addEventListener('keydown', (event) => {
        if (event.altKey && event.key === 'c') {
            searchCounter = MAX_COUNT_WINDOWS;
            localStorage.setItem(KEY_COUNTER, searchCounter);
        }
    });
})();
